// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/type/simple_function_api.h>
#include <pollux/type/type.h>
#include <pollux/vector/complex_vector.h>

namespace kumo::pollux {
    // Maps TypeKind to the corresponding writable vector.
    template<TypeKind K>
    struct KindToFlatVector {
        using type = FlatVector<typename TypeTraits<K>::NativeType>;
        using WrapperType = typename TypeTraits<K>::NativeType;
        using HashRowType = typename TypeTraits<K>::NativeType;
    };

    template<>
    struct KindToFlatVector<TypeKind::TIMESTAMP> {
        using type = FlatVector<Timestamp>;
        using WrapperType = Timestamp;
        using HashRowType = Timestamp;
    };

    template<>
    struct KindToFlatVector<TypeKind::MAP> {
        using type = MapVector;
        using WrapperType = ComplexType;
        using HashRowType = StringView;
    };

    template<>
    struct KindToFlatVector<TypeKind::ARRAY> {
        using type = ArrayVector;
        using WrapperType = ComplexType;
        using HashRowType = StringView;
    };

    template<>
    struct KindToFlatVector<TypeKind::ROW> {
        using type = RowVector;
        using WrapperType = ComplexType;
        using HashRowType = StringView;
    };

    template<>
    struct KindToFlatVector<TypeKind::VARCHAR> {
        using type = FlatVector<StringView>;
        using WrapperType = StringView;
        using HashRowType = StringView;
    };

    template<>
    struct KindToFlatVector<TypeKind::VARBINARY> {
        using type = FlatVector<StringView>;
        using WrapperType = StringView;
        using HashRowType = StringView;
    };

    template<>
    struct KindToFlatVector<TypeKind::OPAQUE> {
        using type = FlatVector<std::shared_ptr<void> >;
        using WrapperType = std::shared_ptr<void>;
        using HashRowType = void;
    };

    template<typename T>
    struct TypeToFlatVector {
        using type = typename KindToFlatVector<SimpleTypeTrait<T>::typeKind>::type;
    };

    // Generic's, by design, do not have any compile time type information, so it is
    // impossible to determine what sort of Vector would hold values for this type.
    // To work around this, we just return BaseVector, since any Vector class can be
    // safely casted to BaseVector, and it is consistent with classes specialized
    // for the Generic type, like the VectorWriter.
    template<typename T, bool comparable, bool orderable>
    struct TypeToFlatVector<Generic<T, comparable, orderable> > {
        using type = BaseVector;
    };
} // namespace kumo::pollux
